const fs = require('fs')
const path = require('path')
const express = require('express')
const app = express()
const favicon = require('serve-favicon')
const bodyParser = require('body-parser')
const resolve = file =>path.resolve(__dirname, file)
// var config = require('../build/webpack.base.config')
const api = require('./api')
const user = require('./user')
// const feapi = require('./fe')
const cookieParser = require('cookie-parser')
var session = require('express-session');
var RedisStore = require('connect-redis')(session);  //定义redisStore
app.use(cookieParser());
app.use(session({
    store: new RedisStore({
        host: 'localhost',		       //redis服务器地址, 这里用本机代替
        port: 6379,              //自定端口
        ttl: 1000 * 60 * 1 // 过期时间
      }),
      resave: true, // don’t save session if unmodified
      saveUninitialized: false, // don’t create session until something stored
      secret: 'keyboard cats',
      cookie: {maxAge: 1000 * 60 * 1},//30 days
}));




app.all('*', function(req, res, next) {  
    res.header("Access-Control-Allow-Origin", "*");  
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");  
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
    res.header("X-Powered-By",' 3.2.1')  
    res.header("Content-Type", "application/json;charset=utf-8");  
    next();  
});  

// const createBundleRenderer = require('vue-server-renderer').createBundleRenderer

app.set('port', (process.env.port || 8080))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: false}))
app.use(cookieParser())
// app.use('/upload',express.static(resolve('../upload')));
app.use(api)
app.use(user)


// app.get('*', function (req, res) {
//   const html = fs.readFileSync(resolve('../index.html'), 'utf-8')
//   res.send(html)
// })

app.listen(app.get('port'), function () {
  console.log('Visit http://localhost:' + app.get('port'))
})
